package com.pr.stream;

import com.pr.entity.Student;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @description:
 * @projectName:spring-skill
 * @see:com.pr.stream
 * @author:LXC
 * @createTime:2024/9/26 14:28
 * @version:1.0
 */
public class StreamTest {

    // 从集合创建 Stream
    List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
    Stream<String> streamFromList = names.stream();

    // 从数组创建 Stream
    String[] nameArray = {"Alice", "Bob", "Charlie"};
    Stream<String> streamFromArray = Arrays.stream(nameArray);

    List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);

    // 使用 Stream.of 创建 Stream
    Stream<String> streamOf = Stream.of("Alice", "Bob", "Charlie");

    @Test
    public void filterTest() {
        names.stream()
                .filter(name -> name.startsWith("A"))  // 过滤出以 "A" 开头的名字
                .forEach(System.out::println);          // 输出：Alice
    }

    @Test
    public void mapTest() {
        names.stream()
                .map(String::toUpperCase)  // 将每个元素转换为大写
                .forEach(System.out::println);  // 输出：ALICE, BOB, CHARLIE
    }

    @Test
    public void sortedTest() {
        names.stream()
                .sorted()  // 按字母顺序排序
                .forEach(System.out::println);  // 输出：Alice, Bob, Charlie
    }

    @Test
    public void distinctTest() {
        numbers.stream()
                .distinct()  // 去重
                .forEach(System.out::println);  // 输出：1, 2, 3, 4, 5
    }

    @Test
    public void foreachTest() {
        names.stream()
                .forEach(System.out::println);  // 输出每个名字
    }

    @Test
    public void collectTest() {
        List<String> filteredNames = names.stream()
                .filter(name -> name.length() > 3)  // 过滤名字长度大于3的
                .collect(Collectors.toList());  // 收集到 List
        System.out.println(filteredNames);  // 输出：[Alice, Char
    }

    @Test
    public void reduceTest() {
        int sum = numbers.stream()
                .reduce(0, Integer::sum);  // 累加求和
        System.out.println(sum);  //
    }

    @Test
    public void countTest() {
        long count = names.stream()
                .filter(name -> name.length() > 3)
                .count();  // 统计名字长度大于3的数量
        System.out.println(count);  // 输出：2
    }

    @Test
    public void parallelTest() {
        names.parallelStream()
                .forEach(System.out::println);  // 并行处理每个元素
    }

    @Test
    public void stramTest(){
        List<Student> students = Arrays.asList(
                new Student("Alice", 90),
                new Student("Bob", 75),
                new Student("Charlie", 85)
        );

        List<String> topStudents = students.stream()
                .filter(student -> student.getScore() > 80)  // 筛选出成绩大于80的学生
                .sorted(Comparator.comparing(Student::getName))  // 按名字排序
                .map(Student::getName)  // 提取名字
                .collect(Collectors.toList());  // 收集到List

        System.out.println(topStudents);  // 输出：[Alice, Charlie]
    }
}
